package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.McRuleInfoMapper;
import cc.rengu.igas.momp.common.entity.*;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/3/25 21:28
 * @description 营销活动规则数据库操作接口实现
 */
public class McRuleInfoMapperImpl implements McRuleInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * @param instId
     * @param mcNo
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcAgentProfitSharingRuleInfo>
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/3/25 23:47
     * @description 代理商营销活动规则信息查询接口实现
     */
    @Override
    public List<TMMcAgentProfitSharingRuleInfo> selectAgentProfitSharingRuleInfoList(String instId, String mcNo) throws Exception {

        String queryClause = "INST_ID=[" + instId + "], MC_NO=[" + mcNo + "]";

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectAgentProfitSharingMcRuleInfoList");

        // 按接口赋查询条件
        TMMcAgentProfitSharingRuleInfo mcAgentProfitSharingRuleInfo = new TMMcAgentProfitSharingRuleInfo();
        mcAgentProfitSharingRuleInfo.setInstId(instId);
        mcAgentProfitSharingRuleInfo.setMcNo(mcNo);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mcAgentProfitSharingRuleInfo, TMMcAgentProfitSharingRuleInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件<{}>获取代理商营销活动规则信息列表失败!", queryClause);
            return null;
        }

        return objectList.stream().map(item -> (TMMcAgentProfitSharingRuleInfo) (item)).collect(Collectors.toList());
    }

    /**
     * @param instId
     * @param mcNo
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcCashierCashbackRuleInfo>
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/3/26 00:02
     * @description 收银员返现营销活动规则信息查询接口实现
     */
    @Override
    public List<TMMcCashierCashbackRuleInfo> selectCashierCashbackRuleInfoList(String instId, String mcNo) throws Exception {

        String queryClause = "INST_ID=[" + instId + "], MC_NO=[" + mcNo + "]";

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCashierCashbackMcRuleInfoList");

        // 按接口赋查询条件
        TMMcCashierCashbackRuleInfo mcCashierCashbackRuleInfo = new TMMcCashierCashbackRuleInfo();
        mcCashierCashbackRuleInfo.setInstId(instId);
        mcCashierCashbackRuleInfo.setMcNo(mcNo);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mcCashierCashbackRuleInfo, TMMcCashierCashbackRuleInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件<{}>获取收银员返现营销活动规则信息列表失败!", queryClause);
            return null;
        }

        return objectList.stream().map(item -> (TMMcCashierCashbackRuleInfo) (item)).collect(Collectors.toList());
    }

    /**
     * @param instId
     * @param mcNo
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcCouponRuleInfo>
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/3/26 00:02
     * @description 卡券营销活动规则信息查询接口实现
     */
    @Override
    public List<TMMcCouponRuleInfo> selectCouponRuleInfoList(String instId, String mcNo) throws Exception {

        String queryClause = "INST_ID=[" + instId + "], MC_NO=[" + mcNo + "]";

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCouponMcRuleInfoList");

        // 按接口赋查询条件
        TMMcCouponRuleInfo mcCouponRuleInfo = new TMMcCouponRuleInfo();
        mcCouponRuleInfo.setInstId(instId);
        mcCouponRuleInfo.setMcNo(mcNo);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mcCouponRuleInfo, TMMcCouponRuleInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件<{}>获取卡券营销活动规则信息列表失败!", queryClause);
            return null;
        }

        return objectList.stream().map(item -> (TMMcCouponRuleInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 商户手续费优惠营销活动规则查询数据库操作接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcMchntFeeDiscountRuleInfo> 商户手续费减免类营销活动规则信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/3/28 18:58
     */
    @Override
    public List<TMMcMchntFeeDiscountRuleInfo> selectMchntFeeDiscountRuleInfoList(String instId, String mcNo) throws Exception {

        String queryClause = "INST_ID=[" + instId + "], MC_NO=[" + mcNo + "]";

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntFeeDiscountMcRuleInfoList");

        // 按接口赋查询条件
        TMMcMchntFeeDiscountRuleInfo mcMchntFeeDiscountRuleInfo = new TMMcMchntFeeDiscountRuleInfo();
        mcMchntFeeDiscountRuleInfo.setInstId(instId);
        mcMchntFeeDiscountRuleInfo.setMcNo(mcNo);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mcMchntFeeDiscountRuleInfo, TMMcMchntFeeDiscountRuleInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件<{}>获取商户手续费优惠营销活动规则信息列表失败!", queryClause);
            return null;
        }

        return objectList.stream().map(item -> (TMMcMchntFeeDiscountRuleInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 减免营销活动规则信息查询接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcMoneyOffRuleInfo>
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 22:11
     */
    @Override
    public List<TMMcMoneyOffRuleInfo> selectMoneyOffRuleInfoList(String instId, String mcNo) throws Exception {

        String queryClause = "INST_ID=[" + instId + "], MC_NO=[" + mcNo + "]";

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMoneyOffMcRuleInfoList");

        // 按接口赋查询条件
        TMMcMoneyOffRuleInfo mcMoneyOffRuleInfo = new TMMcMoneyOffRuleInfo();
        mcMoneyOffRuleInfo.setInstId(instId);
        mcMoneyOffRuleInfo.setMcNo(mcNo);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mcMoneyOffRuleInfo, TMMcMoneyOffRuleInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件<{}>获取减免营销活动规则信息列表失败!", queryClause);
            return null;
        }

        return objectList.stream().map(item -> (TMMcMoneyOffRuleInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 代理商营销活动规则信息新增接口实现
     *
     * @param instId                           法人机构号
     * @param mcAgentProfitSharingRuleInfoList
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/1 22:12
     */
    @Override
    public int insertAgentProfitSharingRuleInfoListByBank(String instId, List<TMMcAgentProfitSharingRuleInfo> mcAgentProfitSharingRuleInfoList) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertAgentProfitSharingRuleInfoListByBank");

        for (TMMcAgentProfitSharingRuleInfo mcAgentProfitSharingRuleInfo : mcAgentProfitSharingRuleInfoList) {

            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mcAgentProfitSharingRuleInfo, TMMcAgentProfitSharingRuleInfo.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("插入机构编码<{}>营销活动编号<{}>的代理商营销活动规则失败!<{}>", mcAgentProfitSharingRuleInfo.getInstId(), mcAgentProfitSharingRuleInfo.getMcNo(), iReturnCode);
                return iReturnCode;
            }
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 代理商营销活动规则信息修改接口实现
     *
     * @param instId                           法人机构号
     * @param mcAgentProfitSharingRuleInfoList
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/1 22:12
     */
    @Override
    public int updateAgentProfitSharingRuleInfoListByBank(String instId, List<TMMcAgentProfitSharingRuleInfo> mcAgentProfitSharingRuleInfoList) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateAgentProfitSharingRuleInfoListByBank");

        for (TMMcAgentProfitSharingRuleInfo agentProfitSharingRuleInfoList : mcAgentProfitSharingRuleInfoList) {

            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, agentProfitSharingRuleInfoList, TMMcAgentProfitSharingRuleInfo.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("更新法人机构号<{}>营销活动编号<{}>规则顺序号<{}>的营销活动规则信息失败!<{}>", agentProfitSharingRuleInfoList.getInstId(), agentProfitSharingRuleInfoList.getMcNo(), agentProfitSharingRuleInfoList.getRuleIndex(), iReturnCode);
                return iReturnCode;
            }
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 代理商营销活动规则信息删除接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/1 22:12
     */
    @Override
    public int deleteAgentProfitSharingRuleInfoListByBank(String instId, String mcNo) throws Exception {
        return 0;
    }

    /**
     * 新增单条代理商类营销活动规则数据库操作接口实现
     *
     * @param instId                       法人机构号
     * @param mcAgentProfitSharingRuleInfo
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int insertAgentProfitSharingRuleInfo(String instId, TMMcAgentProfitSharingRuleInfo mcAgentProfitSharingRuleInfo) throws Exception {
        return 0;
    }

    /**
     * 更新单条代理商类营销活动规则数据库操作接口实现
     *
     * @param instId                       法人机构号
     * @param mcAgentProfitSharingRuleInfo
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int updateAgentProfitSharingRuleInfo(String instId, TMMcAgentProfitSharingRuleInfo mcAgentProfitSharingRuleInfo) throws Exception {
        return 0;
    }

    /**
     * 使用主键删除单条代理商类营销活动规则数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mcNo      营销活动编号
     * @param ruleIndex
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int deleteAgentProfitSharingRuleInfoByPrimaryKey(String instId, String mcNo, String ruleIndex) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "deleteAgentProfitSharingRuleInfoByPrimaryKey");

        TMMcAgentProfitSharingRuleInfo tMMcAgentProfitSharingRuleInfo = new TMMcAgentProfitSharingRuleInfo();
        tMMcAgentProfitSharingRuleInfo.setInstId(instId);
        tMMcAgentProfitSharingRuleInfo.setMcNo(mcNo);
        tMMcAgentProfitSharingRuleInfo.setRuleIndex(ruleIndex);

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tMMcAgentProfitSharingRuleInfo, TMMcMoneyOffRuleInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode && Database.DBS_NOT_FOUND != iReturnCode) {
            rglog.error("删除法人机构号<{}>营销活动编号<{}>的营销活动规则信息失败!<{}>", tMMcAgentProfitSharingRuleInfo.getInstId(), tMMcAgentProfitSharingRuleInfo.getMcNo(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 使用主键查询单条代理商类营销活动规则数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mcNo      营销活动编号
     * @param ruleIndex
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int selectAgentProfitSharingRuleInfoByPrimaryKey(String instId, String mcNo, String ruleIndex) throws Exception {
        return 0;
    }


    /**
     * 收银员换返现营销活动规则信息新增接口实现
     *
     * @param mcCashierCashbackRuleInfoList
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/1 22:12
     */
    @Override
    public int insertCashierCashbackRuleInfoListByBank(List<TMMcCashierCashbackRuleInfo> mcCashierCashbackRuleInfoList) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertCashierCashbackRuleInfoListByBank");

        for (TMMcCashierCashbackRuleInfo tmCashierCashbackRuleInfo : mcCashierCashbackRuleInfoList) {

            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmCashierCashbackRuleInfo, TMMcMoneyOffRuleInfo.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("插入法人机构号<{}>营销活动编号<{}>规则顺序号<{}>的营销活动规则信息失败!<{}>", tmCashierCashbackRuleInfo.getInstId(), tmCashierCashbackRuleInfo.getMcNo(), tmCashierCashbackRuleInfo.getRuleIndex(), iReturnCode);
                return iReturnCode;
            }
        }
        return Database.DBS_SUCCESS;
    }

    /**
     * 收银员换返现营销活动规则信息修改接口实现
     *
     * @param instId                        法人机构号
     * @param mcCashierCashbackRuleInfoList
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/1 22:12
     */
    @Override
    public int updateCashierCashbackRuleInfoListByBank(String instId, List<TMMcCashierCashbackRuleInfo> mcCashierCashbackRuleInfoList) throws Exception {
        return 0;
    }

    /**
     * 收银员换返现营销活动规则信息删除接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/1 22:12
     */
    @Override
    public int deleteCashierCashbackRuleInfoListByMcNo(String instId, String mcNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "deleteCashierCashbackRuleInfoListByMcNo");

        TMMcCashierCashbackRuleInfo tmMcCashierCashbackRuleInfo = new TMMcCashierCashbackRuleInfo();
        tmMcCashierCashbackRuleInfo.setInstId(instId);
        tmMcCashierCashbackRuleInfo.setMcNo(mcNo);

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcCashierCashbackRuleInfo, TMMcCashierCashbackRuleInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode && Database.DBS_NOT_FOUND != iReturnCode) {
            rglog.error("删除法人机构号<{}>营销活动编号<{}>的营销活动规则信息失败!<{}>", tmMcCashierCashbackRuleInfo.getInstId(), tmMcCashierCashbackRuleInfo.getMcNo(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 新增单条收银员换返现类营销活动规则数据库操作接口实现
     *
     * @param instId                    法人机构号
     * @param mcCashierCashbackRuleInfo
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int insertCashierCashbackRuleInfo(String instId, TMMcCashierCashbackRuleInfo mcCashierCashbackRuleInfo) throws Exception {
        return 0;
    }

    /**
     * 更新单条收银员换返现类营销活动规则数据库操作接口实现
     *
     * @param instId                    法人机构号
     * @param mcCashierCashbackRuleInfo
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int updateCashierCashbackRuleInfo(String instId, TMMcCashierCashbackRuleInfo mcCashierCashbackRuleInfo) throws Exception {
        return 0;
    }

    /**
     * 使用主键删除单条收银员换返现类营销活动规则数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mcNo      营销活动编号
     * @param ruleIndex
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int deleteCashierCashbackRuleInfoByPrimaryKey(String instId, String mcNo, String ruleIndex) throws Exception {
        return 0;
    }

    /**
     * 使用主键查询单条收银员换返现类营销活动规则数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mcNo      营销活动编号
     * @param ruleIndex
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int selectCashierCashbackRuleInfoByPrimaryKey(String instId, String mcNo, String ruleIndex) throws Exception {
        return 0;
    }


    /**
     * 卡券类营销活动规则信息新增接口实现
     *
     * @param mcCouponRuleInfoList 卡券类营销活动规则信息表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 22:12
     */
    @Override
    public int insertCouponRuleInfoListByBank(List<TMMcCouponRuleInfo> mcCouponRuleInfoList) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertCouponRuleInfoListByBank");

        for (TMMcCouponRuleInfo tmMcCouponRuleInfo : mcCouponRuleInfoList) {

            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcCouponRuleInfo, TMMcCouponRuleInfo.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("插入法人机构号<{}>营销活动编号<{}>规则顺序号<{}>的营销活动规则信息失败!<{}>", tmMcCouponRuleInfo.getInstId(), tmMcCouponRuleInfo.getMcNo(), tmMcCouponRuleInfo.getRuleIndex(), iReturnCode);
                return iReturnCode;
            }
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 卡券类营销活动规则信息修改接口实现
     *
     * @param mcCouponRuleInfoList
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/1 22:12
     */
    @Override
    public int updateCouponRuleInfoListByBank(List<TMMcCouponRuleInfo> mcCouponRuleInfoList) throws Exception {
        return 0;
    }

    /**
     * 卡券类营销活动规则信息删除接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/1 22:12
     */
    @Override
    public int deleteCouponRuleInfoListByBank(String instId, String mcNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "deleteCouponRuleInfoListByBank");

        TMMcCouponRuleInfo tmMcCouponRuleInfo = new TMMcCouponRuleInfo();
        tmMcCouponRuleInfo.setInstId(instId);
        tmMcCouponRuleInfo.setMcNo(mcNo);

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcCouponRuleInfo, TMMcCouponRuleInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode && Database.DBS_NOT_FOUND != iReturnCode) {
            rglog.error("删除法人机构号<{}>营销活动编号<{}>的营销活动规则信息失败!<{}>", tmMcCouponRuleInfo.getInstId(), tmMcCouponRuleInfo.getMcNo(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 新增单条卡券类类营销活动规则数据库操作接口实现
     *
     * @param instId           法人机构号
     * @param mcCouponRuleInfo
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int insertCouponRuleInfo(String instId, TMMcCouponRuleInfo mcCouponRuleInfo) throws Exception {
        return 0;
    }

    /**
     * 更新单条卡券类类营销活动规则数据库操作接口实现
     *
     * @param instId           法人机构号
     * @param mcCouponRuleInfo
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int updateCouponRuleInfo(String instId, TMMcCouponRuleInfo mcCouponRuleInfo) throws Exception {
        return 0;
    }

    /**
     * 使用主键删除单条卡券类类营销活动规则数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mcNo      营销活动编号
     * @param ruleIndex
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int deleteCouponRuleInfoByPrimaryKey(String instId, String mcNo, String ruleIndex) throws Exception {
        return 0;
    }

    /**
     * 使用主键查询单条卡券类营销活动规则数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mcNo      营销活动编号
     * @param ruleIndex 规则顺序号
     * @return cc.rengu.igas.momp.common.entity.TMMcCouponRuleInfo 卡券类营销活动规则信息表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/6 22:50
     */
    @Override
    public TMMcCouponRuleInfo selectCouponRuleInfoByPrimaryKey(String instId, String mcNo, String ruleIndex) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCouponRuleInfoByPrimaryKey");

        TMMcCouponRuleInfo tmMcCouponRuleInfo = new TMMcCouponRuleInfo();
        tmMcCouponRuleInfo.setInstId(instId);
        tmMcCouponRuleInfo.setMcNo(mcNo);
        tmMcCouponRuleInfo.setRuleIndex(ruleIndex);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMcCouponRuleInfo, tmMcCouponRuleInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>获取卡券营销活动<{}>的规则顺序号<{}>的信息失败!<{}>", instId, mcNo, ruleIndex, iReturnCode);
            return null;
        }

        return tmMcCouponRuleInfo;
    }


    /**
     * 商户手续费优惠营销活动规则信息新增接口实现
     *
     * @param mcMchntFeeDiscountRuleInfoList 商户手续费减免类营销活动规则信息表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 22:12
     */
    @Override
    public int insertMfdRuleInfoListByBank(List<TMMcMchntFeeDiscountRuleInfo> mcMchntFeeDiscountRuleInfoList) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMfdRuleInfoListByBank");

        for (TMMcMchntFeeDiscountRuleInfo tmMcMchntFeeDiscountRuleInfo : mcMchntFeeDiscountRuleInfoList) {

            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcMchntFeeDiscountRuleInfo, TMMcMchntFeeDiscountRuleInfo.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("插入法人机构号<{}>营销活动编号<{}>规则顺序号<{}>的营销活动规则信息失败!<{}>", tmMcMchntFeeDiscountRuleInfo.getInstId(), tmMcMchntFeeDiscountRuleInfo.getMcNo(), tmMcMchntFeeDiscountRuleInfo.getRuleIndex(), iReturnCode);
                return iReturnCode;
            }
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 商户手续费优惠营销活动规则信息修改接口实现
     *
     * @param instId                         法人机构号
     * @param mcMchntFeeDiscountRuleInfoList
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/1 22:12
     */
    @Override
    public int updateMchntFeeDiscountRuleInfoListByBank(String instId, List<TMMcMchntFeeDiscountRuleInfo> mcMchntFeeDiscountRuleInfoList) throws Exception {
        return 0;
    }

    /**
     * 商户手续费优惠营销活动规则信息删除接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 22:12
     */
    @Override
    public int deleteMfdRuleInfoListByBank(String instId, String mcNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "deleteMfdRuleInfoListByBank");

        TMMcMoneyOffRuleInfo tmMcMoneyOffRuleInfo = new TMMcMoneyOffRuleInfo();
        tmMcMoneyOffRuleInfo.setInstId(instId);
        tmMcMoneyOffRuleInfo.setMcNo(mcNo);

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcMoneyOffRuleInfo, TMMcMoneyOffRuleInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode && Database.DBS_NOT_FOUND != iReturnCode) {
            rglog.error("删除法人机构号<{}>营销活动编号<{}>的营销活动规则信息失败!<{}>", tmMcMoneyOffRuleInfo.getInstId(), tmMcMoneyOffRuleInfo.getMcNo(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 新增单条商户手续费优惠类营销活动规则数据库操作接口实现
     *
     * @param instId                     法人机构号
     * @param mcMchntFeeDiscountRuleInfo
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int insertMchntFeeDiscountRuleInfo(String instId, TMMcMchntFeeDiscountRuleInfo mcMchntFeeDiscountRuleInfo) throws Exception {
        return 0;
    }

    /**
     * 更新单条商户手续费优惠类营销活动规则数据库操作接口实现
     *
     * @param instId                     法人机构号
     * @param mcMchntFeeDiscountRuleInfo
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int updateMchntFeeDiscountRuleInfo(String instId, TMMcMchntFeeDiscountRuleInfo mcMchntFeeDiscountRuleInfo) throws Exception {
        return 0;
    }

    /**
     * 使用主键删除单条商户手续费优惠类营销活动规则数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mcNo      营销活动编号
     * @param ruleIndex
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int deleteMchntFeeDiscountRuleInfoByPrimaryKey(String instId, String mcNo, String ruleIndex) throws Exception {
        return 0;
    }

    /**
     * 使用主键查询单条商户手续费优惠类营销活动规则数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mcNo      营销活动编号
     * @param ruleIndex 规则顺序号
     * @return cc.rengu.igas.momp.common.entity.TMMcMchntFeeDiscountRuleInfo
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public TMMcMchntFeeDiscountRuleInfo selectMchntFeeDiscountRuleInfoByPrimaryKey(String instId, String mcNo, String ruleIndex) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntFeeDiscountRuleInfoByPrimaryKey");

        TMMcMchntFeeDiscountRuleInfo tmMcMchntFeeDiscountRuleInfo = new TMMcMchntFeeDiscountRuleInfo();
        tmMcMchntFeeDiscountRuleInfo.setInstId(instId);
        tmMcMchntFeeDiscountRuleInfo.setMcNo(mcNo);
        tmMcMchntFeeDiscountRuleInfo.setRuleIndex(ruleIndex);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMcMchntFeeDiscountRuleInfo, tmMcMchntFeeDiscountRuleInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>获取卡券营销活动<{}>的规则顺序号<{}>的信息失败!<{}>", instId, mcNo, ruleIndex, iReturnCode);
            return null;
        }

        return tmMcMchntFeeDiscountRuleInfo;
    }


    /**
     * 减免营销活动规则信息新增接口实现
     *
     * @param mcMoneyOffRuleInfoList 减免类营销活动规则表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/1 22:12
     */
    @Override
    public int insertMoneyOffRuleInfoListByBank(List<TMMcMoneyOffRuleInfo> mcMoneyOffRuleInfoList) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMoneyOffRuleInfoListByBank");

        for (TMMcMoneyOffRuleInfo tmMcMoneyOffRuleInfo : mcMoneyOffRuleInfoList) {

            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcMoneyOffRuleInfo, TMMcMoneyOffRuleInfo.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("插入法人机构号<{}>营销活动编号<{}>规则顺序号<{}>的营销活动规则信息失败!<{}>", tmMcMoneyOffRuleInfo.getInstId(), tmMcMoneyOffRuleInfo.getMcNo(), tmMcMoneyOffRuleInfo.getRuleIndex(), iReturnCode);
                return iReturnCode;
            }
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 减免营销活动规则信息修改接口实现
     *
     * @param mcMoneyOffRuleInfoList
     * @return int 返回码
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/1 22:12
     */
    @Override
    public int updateMoneyOffRuleInfoListByBank(List<TMMcMoneyOffRuleInfo> mcMoneyOffRuleInfoList) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMoneyOffRuleInfoListByBank");

        for (TMMcMoneyOffRuleInfo tmMcMoneyOffRuleInfo : mcMoneyOffRuleInfoList) {

            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcMoneyOffRuleInfo, TMMcMoneyOffRuleInfo.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("更新法人机构号<{}>营销活动编号<{}>规则顺序号<{}>的营销活动规则信息失败!<{}>", tmMcMoneyOffRuleInfo.getInstId(), tmMcMoneyOffRuleInfo.getMcNo(), tmMcMoneyOffRuleInfo.getRuleIndex(), iReturnCode);
                return iReturnCode;
            }
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 减免营销活动规则信息删除接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return int 返回码
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/1 22:12
     */
    @Override
    public int deleteMoneyOffRuleInfoListByBank(String instId, String mcNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "deleteMoneyOffRuleInfoListByBank");

        TMMcMoneyOffRuleInfo tmMcMoneyOffRuleInfo = new TMMcMoneyOffRuleInfo();
        tmMcMoneyOffRuleInfo.setInstId(instId);
        tmMcMoneyOffRuleInfo.setMcNo(mcNo);

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmMcMoneyOffRuleInfo, TMMcMoneyOffRuleInfo.class);
        if (Database.DBS_SUCCESS != iReturnCode && Database.DBS_NOT_FOUND != iReturnCode) {
            rglog.error("删除法人机构号<{}>营销活动编号<{}>的营销活动规则信息失败!<{}>", tmMcMoneyOffRuleInfo.getInstId(), tmMcMoneyOffRuleInfo.getMcNo(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 新增单条减免类营销活动规则数据库操作接口实现
     *
     * @param instId             法人机构号
     * @param mcMoneyOffRuleInfo
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int insertMoneyOffRuleInfo(String instId, TMMcMoneyOffRuleInfo mcMoneyOffRuleInfo) throws Exception {
        return 0;
    }

    /**
     * 更新单条减免类营销活动规则数据库操作接口实现
     *
     * @param instId             法人机构号
     * @param mcMoneyOffRuleInfo
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int updateMoneyOffRuleInfo(String instId, TMMcMoneyOffRuleInfo mcMoneyOffRuleInfo) throws Exception {
        return 0;
    }

    /**
     * 使用主键删除单条减免类营销活动规则数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mcNo      营销活动编号
     * @param ruleIndex
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int deleteMoneyOffRuleInfoByPrimaryKey(String instId, String mcNo, String ruleIndex) throws Exception {
        return 0;
    }

    /**
     * 使用主键查询单条减免类营销活动规则数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mcNo      营销活动编号
     * @param ruleIndex
     * @return int
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/2 00:02
     */
    @Override
    public int selectMoneyOffRuleInfoByPrimaryKey(String instId, String mcNo, String ruleIndex) throws Exception {
        return 0;
    }

    /**
     * 合并支付使用-根据营销活动编号和订单金额取符合条件的营销活动规则数据库操作接口定义
     *
     * @param instId
     * @param mcNo
     * @param criteria
     * @return cc.rengu.igas.momp.common.entity.TMMcMoneyOffRuleInfo
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/14 17:10
     */
    @Override
    public TMMcMoneyOffRuleInfo selectMoneyOffRuleInfoListOrderByCriteria(String instId, String mcNo, String criteria) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMoneyOffRuleInfoListOrderByCriteria");

        TMMcMoneyOffRuleInfo tmMcMoneyOffRuleInfo = new TMMcMoneyOffRuleInfo();
        tmMcMoneyOffRuleInfo.setInstId(instId);
        tmMcMoneyOffRuleInfo.setMcNo(mcNo);
        tmMcMoneyOffRuleInfo.setCriteria(criteria);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMcMoneyOffRuleInfo, tmMcMoneyOffRuleInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>获取减免类营销活动<{}>规则信息失败!<{}>", instId, mcNo, iReturnCode);
            return null;
        }

        return tmMcMoneyOffRuleInfo;
    }

    /**
     * 合并支付使用-根据营销活动编号和当前统计值取符合条件的商户手续费优惠营销活动规则数据库操作接口实现
     *
     * @param instId
     * @param mcNo
     * @param criteria
     * @return cc.rengu.igas.momp.common.entity.TMMcMchntFeeDiscountRuleInfo
     * @throws Exception
     * @author Jinan Liu
     * @date 2020/4/20 17:53
     */
    @Override
    public TMMcMchntFeeDiscountRuleInfo selectMchntFeeDiscountRuleInfoListOrderByAmount(String instId, String mcNo, String criteria) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntFeeDiscountRuleInfoListOrderByAmount");

        TMMcMchntFeeDiscountRuleInfo tmMcMchntFeeDiscountRuleInfo = new TMMcMchntFeeDiscountRuleInfo();
        tmMcMchntFeeDiscountRuleInfo.setInstId(instId);
        tmMcMchntFeeDiscountRuleInfo.setMcNo(mcNo);
        tmMcMchntFeeDiscountRuleInfo.setCriteriaFloor(criteria);
        tmMcMchntFeeDiscountRuleInfo.setCriteriaCelling(criteria);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMcMchntFeeDiscountRuleInfo, tmMcMchntFeeDiscountRuleInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>获取商户手续费优惠营销活动<{}>规则信息失败!<{}>", instId, mcNo, iReturnCode);
            return null;
        }

        return tmMcMchntFeeDiscountRuleInfo;
    }

    /**
     * 手续费优惠使用-根据营销活动编号查询营销活动规则数据库操作接口实现
     *
     * @param instId
     * @param mcNo
     * @param mchntDeposit
     * @throws Exception
     * @author liujinan
     * @date 2020/4/29
     */
    @Override
    public TMMcMchntFeeDiscountRuleInfo selectMchntFeeDiscountRuleInfoByMcNo(String instId, String mcNo, String mchntDeposit, String criteriaValue) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectMchntFeeDiscountRuleInfoByMcNo");

        TMMcMchntFeeDiscountRuleInfo tmMcMchntFeeDiscountRuleInfo = new TMMcMchntFeeDiscountRuleInfo();
        tmMcMchntFeeDiscountRuleInfo.setInstId(instId);
        tmMcMchntFeeDiscountRuleInfo.setMcNo(mcNo);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tmMcMchntFeeDiscountRuleInfo, TMMcMchntFeeDiscountRuleInfo.class);
        if (null == objectList) {
            rglog.error("根据查询条件法人机构号<{}>获取商户手续费优惠营销活动<{}>规则信息失败!", instId, mcNo);
            return null;
        }

        List<TMMcMchntFeeDiscountRuleInfo> tmMcMchntFeeDiscountRuleInfoList = objectList.stream().map(item -> (TMMcMchntFeeDiscountRuleInfo) (item)).collect(Collectors.toList());

        TMMcMchntFeeDiscountRuleInfo returnTMMcMchntFeeDiscountRuleInfo = new TMMcMchntFeeDiscountRuleInfo();

        if (null != mchntDeposit && !"".equals(mchntDeposit)) {
            for (TMMcMchntFeeDiscountRuleInfo tmMcMchntFeeDiscountRule : tmMcMchntFeeDiscountRuleInfoList) {
                String criteriaFloor = tmMcMchntFeeDiscountRule.getCriteriaFloor();
                String criteriaCelling = tmMcMchntFeeDiscountRule.getCriteriaCelling();
                if ((null != criteriaCelling && !"".equals(criteriaCelling)) && !"0.00".equals(criteriaCelling)) {
                    if (new BigDecimal(mchntDeposit).compareTo(new BigDecimal(criteriaFloor)) > -1
                            && new BigDecimal(mchntDeposit).compareTo(new BigDecimal(criteriaCelling)) < 1) {
                        returnTMMcMchntFeeDiscountRuleInfo = tmMcMchntFeeDiscountRule;
                        break;
                    }
                } else {
                    if (new BigDecimal(mchntDeposit).compareTo(new BigDecimal(criteriaFloor)) > -1) {
                        returnTMMcMchntFeeDiscountRuleInfo = tmMcMchntFeeDiscountRule;
                        break;
                    }
                }
            }
        }
        if (null != criteriaValue && !"".equals(criteriaValue)) {
            for (TMMcMchntFeeDiscountRuleInfo tmMcMchntFeeDiscountRule : tmMcMchntFeeDiscountRuleInfoList) {
                String criteriaFloor = tmMcMchntFeeDiscountRule.getCriteriaFloor();
                String criteriaCelling = tmMcMchntFeeDiscountRule.getCriteriaCelling();
                if ((null != criteriaCelling && !"".equals(criteriaCelling)) && !"0.00".equals(criteriaCelling)) {
                    if (new BigDecimal(criteriaValue).compareTo(new BigDecimal(criteriaFloor)) > -1
                            && new BigDecimal(criteriaValue).compareTo(new BigDecimal(criteriaCelling)) < 1) {
                        returnTMMcMchntFeeDiscountRuleInfo = tmMcMchntFeeDiscountRule;
                        break;
                    }
                } else {
                    if (new BigDecimal(mchntDeposit).compareTo(new BigDecimal(criteriaFloor)) > -1) {
                        returnTMMcMchntFeeDiscountRuleInfo = tmMcMchntFeeDiscountRule;
                        break;
                    }
                }
            }
        }
        return returnTMMcMchntFeeDiscountRuleInfo;
    }

    /**
     * 使用营销活动编号查询卡券总量数据库操作接口实现
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @return cc.rengu.igas.momp.common.entity.TMMcCouponRuleInfo 卡券类营销活动规则信息表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/20 9:42
     */
    @Override
    public TMMcCouponRuleInfo selectCouponAmountByMcNo(String instId, String mcNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "mompSelectCouponAmountByMcNo");

        TMMcCouponRuleInfo tmMcCouponRuleInfo = new TMMcCouponRuleInfo();
        tmMcCouponRuleInfo.setInstId(instId);
        tmMcCouponRuleInfo.setMcNo(mcNo);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMcCouponRuleInfo, tmMcCouponRuleInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>获取卡券营销活动<{}>的卡券总量失败!", instId, mcNo, iReturnCode);
            return null;
        }

        return tmMcCouponRuleInfo;
    }

    /**
     * 根据营销活动编号和规则号取符合条件的营销活动规则数据库操作接口定义
     *
     * @param instId
     * @param mcNo
     * @param ruleIndex
     * @return cc.rengu.igas.momp.common.entity.TMMcMoneyOffRuleInfo
     * @throws Exception
     * @author liujinan
     * @date 2021/6/10 17:10
     */
    @Override
    public TMMcMoneyOffRuleInfo selectMoneyOffRuleInfoOrderByMcNoRuleIndex(String instId, String mcNo, String ruleIndex) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMoneyOffRuleInfoOrderByMcNoRuleIndex");

        TMMcMoneyOffRuleInfo tmMcMoneyOffRuleInfo = new TMMcMoneyOffRuleInfo();
        tmMcMoneyOffRuleInfo.setInstId(instId);
        tmMcMoneyOffRuleInfo.setMcNo(mcNo);
        tmMcMoneyOffRuleInfo.setRuleIndex(ruleIndex);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmMcMoneyOffRuleInfo, tmMcMoneyOffRuleInfo);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>获取减免类营销活动<{}>规则信息失败!<{}>", instId, mcNo, iReturnCode);
            return null;
        }

        return tmMcMoneyOffRuleInfo;
    }
}
